#ifndef HAVE_TEXT2XML_REGEX_H
#define HAVE_TEXT2XML_REGEX_H

#include <regex.h>
#include "Vector.h"

#ifdef __cplusplus
extern "C" {
#endif
  typedef struct _Regex Regex;

  struct _Regex {
    char*    pattern;
    regex_t* compiled1;
    regex_t* compiled2;
    int      flags;
  };

  typedef const char* (*ReSubFunc)(Regex*, const char*, void*);

  /**
   * Returns a new regex.
   */
  Regex* regex_new(const char* pattern, int flags);

  /**
   * Destroys the regex.
   */
  void regex_free(Regex* regex);

  /**
   * Returns True if the given string matches agains the regular expression.
   * Exits with an error if the expression can't be compiled.
   */
  short int regex_matches(Regex* regex, const char* string);

  /**
   * Returns a vector of strings if the regular expression matches at the 
   * beginning of the string.
   * Exits with an error if the expression can't be compiled.
   */
  Vector* regex_match(Regex* regex, const char* string);

  /**
   * Returns a vector of strings if the regular expression matches the string.
   * Exits with an error if the expression can't be compiled.
   */
  Vector* regex_search(Regex* regex, const char* string);

  /**
   * Calls the given function once for each time a match is found in the
   * given string.
   * Exits with an error if the expression can't be compiled.
   */
  short int regex_sub(Regex*      regex,
                      const char* input,
                      char**      dest,
                      ReSubFunc   func,
                      void*       data);
#ifdef __cplusplus
}
#endif                          /* __cplusplus */
#endif                          /* HAVE_TEXT2XML_REGEX_H */
